package com.example.healthmanagement.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.healthmanagement.pojo.Report;
import com.example.healthmanagement.pojo.VO.aVo;
import com.example.healthmanagement.service.ReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author chen
 * @since 2024-03-12
 */
@RestController
@RequestMapping("/report")
public class ReportController {
@Autowired
ReportService reportService;

    public static String extractDateAndTime(String dateTimeString) {
        String date = dateTimeString.substring(5, 10);
        return date;
    }

    private static final Pattern CALORIE_PATTERN = Pattern.compile("今日共摄入能量(\\d+)千卡");

    public static String extractCaloriesString(String content) {
        Matcher matcher = CALORIE_PATTERN.matcher(content);
        if (matcher.find()) {
            return matcher.group(1);
        }
        return "0";
    }
    /**
     * 七天数据
     * @param a
     * @return
     */
    @PostMapping("/statistics")
    public Object statistics(aVo a){
        System.out.println(a);
// 创建QueryWrapper实例
        LambdaQueryWrapper<Report> queryWrapper = new LambdaQueryWrapper<>();

// 获取当前日期的前六天日期，即七天内的数据
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, -6);
        Date sixDaysAgo = calendar.getTime();

// 设置时间条件，只选择创建时间在当前日期前六天内的记录
        queryWrapper.ge(Report::getCreateTime, sixDaysAgo);
        queryWrapper.eq(Report::getChildID,a.getUid());
        List<Report> reportList = reportService.list(queryWrapper);


        List timeArr = new ArrayList();
        List dataArr = new ArrayList();

        // 遍历
        for (Report report : reportList) {
            timeArr.add(extractDateAndTime(report.getCreateTime()));
            dataArr.add(extractCaloriesString(report.getContent()));
        }


        Map<String, Object> map = new HashMap<>();
        map.put("timeList", timeArr);
        map.put("dataList", dataArr);

        return map;
    }



}

